package com.shanzmoo.core.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.base.annotation.IntIn;
import com.shanzmoo.base.constant.RegularExpression;
import com.shanzmoo.base.exception.OutletOrderException;
import com.shanzmoo.db.order.entity.OutletOrderDetailEntity;
import com.shanzmoo.db.order.entity.OutletOrderEntity;
import com.shanzmoo.db.order.entity.dto.OutletOrderDetailSaveDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 门店订单保存对象
 *
 * @author: Gzhao 2020/12/14
 */
@Data
@ApiModel("门店订单保存对象")
public class OutletOrderSaveDto implements Serializable {

    private static final long serialVersionUID = 1758L;

    @ApiModelProperty(value = "订单id")
    private Integer orderId;

    @NotNull(message = "会员id不允许为空")
    @ApiModelProperty(value = "会员id(客户)", example = "1")
    private Integer memberId;

    @ApiModelProperty(value = "折扣", example = "1")
    private BigDecimal discount;

    @ApiModelProperty(value = "结算方式：是否月结", example = "false")
    private Boolean monthly;

    @IntIn(values = {0, 1}, message = "发货类型数据异常")
    @ApiModelProperty(value = "发货类型:0-自提，1-物流", example = "1")
    private Integer deliveryType;

    @Pattern(regexp = RegularExpression.DATE, message = "预计完成时间格式异常")
    @ApiModelProperty(value = "预计完成时间", example = "2021-01-01")
    private String expectFinishDate;

    @Min(0)
    @Max(9999999)
    @NotNull(message = "订单金额不允许为空")
    @ApiModelProperty(value = "订单金额订单金额（单位元）", example = "95")
    private BigDecimal amount;

    @Min(0)
    @Max(9999999)
    @NotNull(message = "实收金额不允许为空")
    @ApiModelProperty(value = "实收金额（单位元）", example = "90")
    private BigDecimal receiveAmount;

    @ApiModelProperty(value = "折扣金额", example = "0")
    private BigDecimal discountAmount;

    @Min(0)
    @Max(9999999)
    @ApiModelProperty(value = "优惠金额（单位元）", example = "5")
    private BigDecimal abatement;

    @ApiModelProperty(value = "优惠原因", example = "抹零")
    private String abatementReason;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "收件人", example = "张三")
    private String consignee;

    @Pattern(regexp = RegularExpression.MOBILE_1,  message = "手机格式异常")
    @ApiModelProperty(value = "收件人手机", example = "13636933693")
    private String consigneeMobile;

    @ApiModelProperty(value = "收件人所在省市区", example = "北京 北京市 朝阳区")
    private String consigneeDistrict;

    @ApiModelProperty(value = "收件人详细地址", example = "国贸309号")
    private String consigneeAddress;

    @NotEmpty(message = "订单明细不允许为空")
    @ApiModelProperty(value = "订单明细")
    private List<OutletOrderDetailSaveDto> orderDetails;

    public OutletOrderEntity covert2OutletOrder(String orderNo) {
        OutletOrderEntity outletOrder = JSON.parseObject(JSON.toJSONString(this), OutletOrderEntity.class);
        outletOrder.setOrderNo(orderNo);
        outletOrder.setOutletId(CurrentUserCache.getOutletId());
        outletOrder.setCreateUser(CurrentUserCache.getUserId());
        outletOrder.setId(this.orderId);
        return outletOrder;
    }

    public List<OutletOrderDetailEntity> covert2OutletOrderDetails(String orderNo) {
        List<OutletOrderDetailEntity> list = JSON.parseArray(JSON.toJSONString(this.orderDetails), OutletOrderDetailEntity.class);
        list.forEach(e -> {
            e.setOrderNo(orderNo);
            e.setCreateUser(CurrentUserCache.getUserId());
            e.setRoot(true == e.getType());

            if (e.getType() && e.getRoot() == null) {
                throw new OutletOrderException("标准商品刷机属性不允许为空");
            }

            // 非标准商品生产 商品编号
            if (!e.getType()){
                e.setSku(generateNonstandardSkuCode());
            }
        });
        return list;
    }

    /**
     * 生成非标准商品sku编码
     * @return
     */
    private String generateNonstandardSkuCode() {
        StringBuilder stb = new StringBuilder("NS");
        // 年月日时分秒
        stb.append(DateUtil.format(LocalDateTime.now(), "yyMMddHHmm"));
        // 随机数
        stb.append(RandomUtil.randomInt(10, 99));
        return stb.toString();
    }
}
